package com.ljm.bootweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@ResponseBody
public class RequestController {

    @GetMapping("/goto")
    public  String goToPage(HttpServletRequest request){

        request.setAttribute("msg","成功了~");
        request.setAttribute("code",200);
        return "forward:/success";
    }

    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute("msg") String msg,
                          @RequestAttribute("code") Integer code,
                          HttpServletRequest request){
        Map<String,Object> mp = new HashMap<>();

        mp.put("msg",msg);
        mp.put("code",code);
//        mp.put("request",request);
        return mp;
    }

    // 获取矩阵变量中的值
    // /cars/sell;low=34;brand=byd,audi,yd
    // SpringBoot 默认禁用了矩阵变量的功能
    // 手动化开启：对于路径的处理。UrlPathHelper进行解析。removeSemicolonContent支持矩阵变量的

    @GetMapping("/cars/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable String path){
        Map<String,Object> mp = new HashMap<>();
        mp.put("low",low);
        mp.put("brand",brand);
        mp.put("path",path);
        return mp;
    }

    // /boss/1;age=20/2;age=10
    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age",pathVar = "bossId")Integer bossAge,
                    @MatrixVariable(value = "age",pathVar = "empId")Integer empAge){

        Map<String,Object> mp = new HashMap<>();
        mp.put("bossAge",bossAge);
        mp.put("empAge",empAge);
        return mp;
    }
}

















